<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/

	$GLOBALS['PERMS'] = 	array(
					'SEND_GLOBAL_MESSAGE'		=>array('SERVERONLY',0x1),
					''	=>array('SERVERONLY',0x2),
					'SEND_SYS_MESSAGE'			=>array('SERVERONLY',0x4),
					'SEND_IM_MESSAGE'			=>array('SERVERONLY',0x8),
					'OVERRIDE_DISABLE'			=>array('SERVERONLY',0x10),
					'GLOBAL_VOICE'				=>array('SERVERONLY',0x20),
					'GLOBAL_ACCESS'				=>array('SERVERONLY',0x40),
					'BAN_IMMUNITY'				=>array('SERVERONLY',0x80),
					'CREATE_ROOM'				=>array('SERVERONLY',0x100),
					'VIEW_HIDDEN_EMAILS'		=>array('SERVERONLY',0x200),
					'LOG_PM'					=>array('SERVERONLY',0x400),
					'SEND_OFFLINE_MESSAGE'		=>array('SERVERONLY',0x800),
					'USE_BUDDY_LIST'			=>array('SERVERONLY',0x1000),
					
					'ACCESS_ROOM'				=>array('DEFAULT',0x1),
					'BECOME_INVISIBLE'			=>array('DEFAULT',0x2),
					'BAN_KICK_USER'				=>array('DEFAULT',0x4),
					'MUTE_USER'					=>array('DEFAULT',0x8),
					'VIEW_IP'					=>array('DEFAULT',0x10),
					'READ_UNVOICED_USERS'		=>array('DEFAULT',0x20),
					'ENTER_FULL_ROOM'			=>array('DEFAULT',0x40),
					'PASSWORD_CORRECT'			=>array('DEFAULT',0x80),
					'SEND_REGULAR_MESSAGE'		=>array('DEFAULT',0x100),
					'EDIT_ROOM_ACCESS'			=>array('DEFAULT',0x200),
					'EDIT_ROOM_SETTINGS'		=>array('DEFAULT',0x400),
					'EDIT_ROOM_PERMS'			=>array('DEFAULT',0x800),
					'SET_ROOM_LOG'				=>array('DEFAULT',0x1000),
					'VIEW_ROOM_LOG'				=>array('DEFAULT',0x2000),
					'SEE_INVISIBLE'				=>array('DEFAULT',0x4000),
					
					'ACP_MANAGEMENT'			=>array('ADMIN',0x1),
					'ACP_EDITUSER'				=>array('ADMIN',0x1),
					'ACP_ACTIVATION'			=>array('ADMIN',0x1),
					'ACP_EDITUSER_USERPROFILE'			=>array('ADMIN',0x1),
					'ACP_EDITUSER_USERSETTINGS'			=>array('ADMIN',0x1),
					'ACP_EDITGROUP_GROUPPERMISSIONS'	=>array('ADMIN',0x2),
					'ACP_EDITUSER_USERPERMISSIONS'		=>array('ADMIN',0x2),
					'ACP_MANAGEGROUPS'			=>array('ADMIN',0x4),
					'ACP_EDITGROUP'				=>array('ADMIN',0x4),
					'ACP_EDITGROUP_GROUPMEMBERS'		=>array('ADMIN',0x4),
					'ACP_UPDATEMANAGER'			=>array('ADMIN',0x8),
					'ACP_MANAGEBANS'			=>array('ADMIN',0x10),
					'ACP_MANAGEROOMS'			=>array('ADMIN',0x20),
					'ACP_EDITROOM'				=>array('ADMIN',0x20),
					'ACP_SETTINGS'				=>array('ADMIN',0x40),
					'ACP_DEFAULTSTYLES'			=>array('ADMIN',0x40),
					'ACP_MANAGEFILTER'			=>array('ADMIN',0x40),
					'ACP_MANAGESMILIES'			=>array('ADMIN',0x40),
					'ACP_EVENTS'				=>array('ADMIN',0x40),
					'ACP_INTEGRATION'			=>array('ADMIN',0x80),
					'ACP_APPEARANCE'			=>array('ADMIN',0x100),
					'ACP_MANAGELANG'			=>array('ADMIN',0x200),
					
					'ACP_TOOLS'			=>array('ADMIN',0x7FFFF),
				);
	
	class lib_user extends lib_clientsidedb{
		var $group;

		function lib_user($initial_id,$getby='uid'){
			if(!is_numeric($initial_id) && $getby == 'uid') $getby = 'name';
			parent::lib_clientsidedb($initial_id,$getby,'uid','users');
			$this->group = new lib_group($this->data_cache['group']);
		}
		
		function get_output(){
			global $ses;

			$out = array(
					'uid'=>$this->data_cache['uid'],
					'name'=>$this->data_cache['name'],
					'group'=>$this->data_cache['group'],
					'status'=>$this->data_cache['status'],
					'perms'=>$this->data_cache['perms'],
				);
			
			return $out;
		}
		
		function commit_changes($changes=null){
			if($changes !== null)
				$this->data_cache = array_merge($this->data_cache,$changes);
			
			parent::commit_changes();
		}

		function remove(){
			if($GLOBALS['CACHE']->exists("private_messages/{$this->data_cache['uid']}"))
				$GLOBALS['CACHE']->remove("private_messages/{$this->data_cache['uid']}");
			
			$GLOBALS['DATABASE']->remove('activation',$this->data_cache['uid']);
				
			parent::remove();
		}

		function ignore_user($uid){
			$user = new lib_user($uid);
			if($user->is_valid() && !isset($this->data_cache['ignore'][$user->data_cache['uid']])){
				$this->data_cache['ignore'][$user->data_cache['uid']] = $user->data_cache['name'];
				$this->commit_changes(array('ignore'=>$this->data_cache['ignore']));
				return $user;
			}

			return false;
		}

		function unignore_user($uid){
			$user = new lib_user($uid);
			if($user->is_valid() && isset($this->data_cache['ignore'][$user->data_cache['uid']])){
				unset($this->data_cache['ignore'][$user->data_cache['uid']]);
				$this->commit_changes(array('ignore'=>$this->data_cache['ignore']));
				return $user;
			}

			return false;
		}

		function add_buddy($uid,$override=false){
			if($override || $this->has_permission('USE_BUDDY_LIST')){
				$user = new lib_user($uid);
				if($user->is_valid() && !isset($this->data_cache['buddies'][$user->data_cache['uid']])){
					$this->data_cache['buddies'][$user->data_cache['uid']] = $user->data_cache['name'];
					$this->commit_changes(array('buddies'=>$this->data_cache['buddies']));
					// TODO: Does this work correctly?  Wouldn't it show them as online even if they are not?
					$GLOBALS['CACHE']->write("private_messages/{$this->data_cache['uid']}",array('lastUpdateTime'=>microtime_float(),'operation'=>'online_add','data'=>array('uid'=>$user->data_cache['uid'],'name'=>$user->data_cache['name'],'rid'=>'buddylist')));
					return $user;
				}
			}

			return false;
		}

		function remove_buddy($uid,$override=false){
			if($override || $this->has_permission('USE_BUDDY_LIST')){
				$user = new lib_user($uid);
				if($user->is_valid() && isset($this->data_cache['buddies'][$user->data_cache['uid']])){
					unset($this->data_cache['buddies'][$user->data_cache['uid']]);
					$this->commit_changes(array('buddies'=>$this->data_cache['buddies']));
					$GLOBALS['CACHE']->write("private_messages/{$this->data_cache['uid']}",array('lastUpdateTime'=>microtime_float(),'operation'=>'online_remove','data'=>array('uid'=>$user->data_cache['uid'],'rid'=>'buddylist')));
					return $user;
				}
			}

			return false;
		}
		
		public function set_permission($permission,$mode,$pgroup=null){
			
			if($pgroup == null){
				if(!isset($GLOBALS['PERMS'][$permission]))
					trigger_error($GLOBALS['LANG']->lang_global->permission_does_not_exist.$permission,E_USER_ERROR);
			
				return $this->set_permission($GLOBALS['PERMS'][$permission][1],$mode,$GLOBALS['PERMS'][$permission][0]);
			}
			
			if(!isset($this->data_cache['perms'][$pgroup]))
				$this->data_cache['perms'][$pgroup] = array('allowed'=>0,'denied'=>0);
				
			$this->data_cache['perms'][$pgroup]['allowed'] &=~ $permission;
			$this->data_cache['perms'][$pgroup]['denied'] &=~ $permission;
				
			switch($mode){
				case -1:
					// Set to denied
					$this->data_cache['perms'][$pgroup]['denied'] |= $permission;
				break;
				case 1:
					// Set to allow
					$this->data_cache['perms'][$pgroup]['allowed'] |= $permission;
				break;
			}
			
		}

		public function has_permission($permission,$group=null){
			
			if($group == null){
				if(!isset($GLOBALS['PERMS'][$permission]))
					trigger_error($GLOBALS['LANG']->lang_global->permission_does_not_exist.$permission,E_USER_ERROR);
				
				return $this->has_permission($GLOBALS['PERMS'][$permission][1],$GLOBALS['PERMS'][$permission][0]);
			}
			
			$gperms = $this->get_pgroup_perms($group);
			
			return ($permission & $gperms['allowed'] &~ $gperms['denied']);
			
		}
		
		public function permission_denied($permission,$group=null){
			
			if($group == null){
				if(!isset($GLOBALS['PERMS'][$permission]))
					trigger_error($GLOBALS['LANG']->lang_global->permission_does_not_exist.$permission,E_USER_ERROR);
				
				return $this->permission_denied($GLOBALS['PERMS'][$permission][1],$GLOBALS['PERMS'][$permission][0]);
			}
			
			$gperms = $this->get_pgroup_perms($group);
			
			return ($permission & $gperms['denied']);
			
		}
		
		public function local_permission_value($permission,$group=null){
			
			if($group == null){
				if(!isset($GLOBALS['PERMS'][$permission]))
					trigger_error($GLOBALS['LANG']->lang_global->permission_does_not_exist.$permission,E_USER_ERROR);
			
				return $this->local_permission_value($GLOBALS['PERMS'][$permission][1],$GLOBALS['PERMS'][$permission][0]);
			}
				
			$luperms = isset($this->data_cache['perms'][$group]) ? $this->data_cache['perms'][$group] : array('allowed'=>0,'denied'=>0);
			
			if(($permission & $luperms['allowed']))
				return 1;
			elseif(($permission & $luperms['denied']))
				return -1;
			else
				return 0;
			
		}
		
		private function get_pgroup_perms($pgroup,$room=null){
			$luperms = isset($this->data_cache['perms'][$pgroup]) ? $this->data_cache['perms'][$pgroup] : array('allowed'=>0,'denied'=>0);
			$lgperms = isset($this->group->data_cache['perms'][$pgroup]) ? $this->group->data_cache['perms'][$pgroup] : array('allowed'=>0,'denied'=>0);
			return array('allowed'=>($luperms['allowed']|$lgperms['allowed']),'denied'=>($luperms['denied']|$lgperms['denied']));
		}
		
		// Why isn't this in lib_user_extended?
		function send_welcome_email($code){
			global $settings;
			
			$reps = array('username'=>$this->data_cache['name'],'sitename'=>$settings['site_name'],'activation_code'=>$code,'site_addr'=>$_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"],'uid'=>$this->data_cache['uid']);
			foreach($reps as $var=>$value){
				$GLOBALS['OUTPUT']->lang["email_register_{$settings['account_activation']}_body"] = str_replace("%$var%",$value,$GLOBALS['OUTPUT']->lang["email_register_{$settings['account_activation']}_body"]);
				$GLOBALS['OUTPUT']->lang["email_register_{$settings['account_activation']}_subject"] = str_replace("%$var%",$value,$GLOBALS['OUTPUT']->lang["email_register_{$settings['account_activation']}_subject"]);
			}

			// TODO: Send out an E-Mail
		}

	}
	
?>